<?php

namespace App\Http\Controllers;

use App\Models\ThesisTopic;
use Illuminate\Http\Request;

class ThesisTopicController extends Controller
{
    public function index()
    {
        $topics = ThesisTopic::with(['teacher.user'])->get();
        return response()->json($topics);
    }

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'requirements' => 'required|string',
            'max_students' => 'required|integer|min:1',
        ]);

        $topic = ThesisTopic::create([
            'teacher_id' => $request->user()->teacher->id,
            'title' => $request->title,
            'description' => $request->description,
            'requirements' => $request->requirements,
            'max_students' => $request->max_students,
        ]);

        return response()->json($topic->load('teacher.user'), 201);
    }

    public function show(ThesisTopic $topic)
    {
        return response()->json($topic->load(['teacher.user', 'topicSelections.student.user']));
    }

    public function update(Request $request, ThesisTopic $topic)
    {
        $request->validate([
            'title' => 'string|max:255',
            'description' => 'string',
            'requirements' => 'string',
            'max_students' => 'integer|min:1',
            'status' => 'in:open,closed',
        ]);

        $topic->update($request->all());
        return response()->json($topic->load('teacher.user'));
    }

    public function destroy(ThesisTopic $topic)
    {
        $topic->delete();
        return response()->json(null, 204);
    }
} 